package com.scu.gkvr_system_backend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scu.gkvr_system_backend.config.DataEngine;
import com.scu.gkvr_system_backend.mapper.GsLiDistMapper;
import com.scu.gkvr_system_backend.mapper.GsLiScoreRankMapper;
import com.scu.gkvr_system_backend.mapper.GsWenDistMapper;
import com.scu.gkvr_system_backend.mapper.GsWenScoreRankMapper;
import com.scu.gkvr_system_backend.pojo.SchoolScore;
import com.scu.gkvr_system_backend.pojo.ScoreRank;
import com.scu.gkvr_system_backend.service.ScoreRankService;
import com.scu.gkvr_system_backend.vo.ScoreLineYearsGetVO;
import com.scu.gkvr_system_backend.vo.ScoreRankGetListVO;
import com.scu.gkvr_system_backend.vo.ScoreRankGetRankVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author qinyc
* @description 针对表【gs_wen_score_rank】的数据库操作Service实现
* @createDate 2024-05-10 23:26:57
*/
@Service
public class ScoreRankServiceImpl extends ServiceImpl<GsWenScoreRankMapper, ScoreRank>
    implements ScoreRankService {

    @Autowired
    private GsWenScoreRankMapper gsWenScoreRankMapper;

    @Autowired
    private GsLiScoreRankMapper gsLiScoreRankMapper;

    @Autowired
    private GsLiDistMapper gsLiDistMapper;

    @Autowired
    private GsWenDistMapper gsWenDistMapper;



    @Override
    public Map<String,Object> getScoreRank(Integer page, String type) {

        Page<ScoreRankGetListVO> scoreRankGetListVOPage = new Page<>(page,20);

        IPage<ScoreRankGetListVO> iPage = null;

        if ("文科".equals(type)){
            iPage = gsWenScoreRankMapper.getScoreRankList(scoreRankGetListVOPage);
        }else{
            iPage = gsLiScoreRankMapper.getScoreRankList(scoreRankGetListVOPage);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("data",iPage.getRecords());
        return map;
    }

    @Override
    public List<ScoreLineYearsGetVO> getScoreLineYears(String type) {

        List<ScoreLineYearsGetVO> result;



        if ("文科".equals(type)){
            List<ScoreRank> list = gsWenScoreRankMapper.getList();
            result = getScoreLineYearsHandler(list);
        }else{
            List<ScoreRank> list = gsLiScoreRankMapper.getList();
            result = getScoreLineYearsHandler(list);
        }
        return result;
    }




    public List<ScoreLineYearsGetVO> getScoreLineYearsHandler(List<ScoreRank> list){
        HashMap<String, Integer> map2019 = new HashMap<>();
        HashMap<String, Integer> map2020 = new HashMap<>();
        HashMap<String, Integer> map2021 = new HashMap<>();
        HashMap<String, Integer> map2022 = new HashMap<>();
        HashMap<String, Integer> map2023 = new HashMap<>();

        list.stream()
                .filter(i->i.getBatch2019()!=null||i.getBatch2020()!=null||i.getBatch2022()!=null||i.getBatch2023()!=null||i.getBatch2021()!=null)
                .sorted((o1,o2)->o2.getScore()-o1.getScore())
                .forEach(i->{
            map2019.put(i.getBatch2019(),i.getScore());
            map2020.put(i.getBatch2020(),i.getScore());
            map2021.put(i.getBatch2021(),i.getScore());
            map2022.put(i.getBatch2022(),i.getScore());
            map2023.put(i.getBatch2023(),i.getScore());
        });
        HashMap<String, List<Integer>> map = new HashMap<>();
        for (HashMap<String, Integer> m : List.of(map2019, map2020, map2021, map2022, map2023)) {
            for (String batch : m.keySet()) {
                if(map.containsKey(batch)){
                    List<Integer> integers = map.get(batch);
                    integers.add(m.get(batch));
                }else{
                    ArrayList<Integer> integers = new ArrayList<>();
                    integers.add(m.get(batch));
                    map.put(batch,integers);
                }
            }
        }
        List<ScoreLineYearsGetVO> vos = new ArrayList<>();
        map.forEach((k,v)->{
            ScoreLineYearsGetVO vo = new ScoreLineYearsGetVO();
            vo.setName(k);
            vo.setData(v);
            vos.add(vo);
        });
        return vos;
    }


    @Override
    public Map<String, Object> getRank(int score, String type) {

        ScoreRankGetRankVO scoreRankGetRankVO = new ScoreRankGetRankVO();

        Integer rank = DataEngine.searchRank(score, type);

        scoreRankGetRankVO.setRankRange(rank);
        scoreRankGetRankVO.setRanking(rank);

        Map<String,Object> map = new HashMap<>();
        map.put("scoreRank", scoreRankGetRankVO);
        return map;
    }

    @Override
    public Map<String, Object> getReco(int page, int score, String risk, String type) {
        Map<String,Object> result = new HashMap<>();

        int rank =DataEngine.searchRank(score,type);

        int UpperRank = (int) (rank * 0.6);
        int LowerRank = (int) (rank * 1.8);
        switch (risk) {
            //根据风险等级确定排名范围，阈值自由设置
            case "可冲击":
                UpperRank = (int) (rank * 0.6);
                LowerRank = (int) (rank * 0.8);
                break;
            case "较稳妥":
                UpperRank = (int) (rank * 0.8);
                LowerRank = (int) (rank * 1.1);
                break;
            case "可保底":
                UpperRank = (int) (rank * 1.2);
                LowerRank = (int) (rank * 1.6);
                break;
            default:
                break;
        }

        List<SchoolScore> schools;

        if ("文科".equals(type)){
            schools = gsWenDistMapper.selectRank(UpperRank, LowerRank);
        }else{
            schools = gsLiDistMapper.selectRank(UpperRank, LowerRank);
        }

        int total = schools.size();
        int startIndex = (page - 1) * 10; //计算起始索引
        int endIndex = Math.min(startIndex + 10, schools.size()); //计算结束索引（最多10个学校）
        schools = schools.subList(startIndex, endIndex); //分页
        List<Integer> averageScores = new ArrayList<>(); //平均分作为预测投档线
        List<Integer> upLineRateList = new ArrayList<>();
        for (SchoolScore scLiScore : schools) {
            double averageScore = ( scLiScore.getScore2019()+ scLiScore.getScore2020() + scLiScore.getScore2021() + scLiScore.getScore2022() + scLiScore.getScore2023()) / 5.0;
            double rankRate = ((scLiScore.getRank2019() + scLiScore.getRank2020() + scLiScore.getRank2021() + scLiScore.getRank2022()+scLiScore.getRank2023()) / 5.0) / rank;
            int UpLineRate = (int) (rankRate * 50) >= 100 ? 99 : (int) (rankRate * 50); //概率计算
            upLineRateList.add(UpLineRate);
            averageScores.add((int) averageScore);
        }
        result.put("total", total);
        result.put("scoreRank", rank);
        result.put("upLineRateList", upLineRateList);
        result.put("averageScores", averageScores);
        result.put("schools", schools);
        return result;
    }


}




